package org.mili.ticketeer.ui.meeting;

import org.freixas.jcalendar.JCalendarCombo;
import org.mili.application.dto.Identifieable;
import org.mili.ticketeer.dto.Meeting;
import org.mili.ticketeer.service.MeetingService;
import org.mili.application.service.ServiceException;
import org.mili.application.service.ServiceFactory;
import org.mili.application.ui.FormElement;
import org.mili.application.ui.SimpleForm;
import org.mili.application.util.Log;

import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * User: papa
 * Date: 20.07.13
 * Time: 14:10
 * To change this template use File | Settings | File Templates.
 */
public class MeetingDataForm extends SimpleForm<Meeting> {
    private static MeetingService meetingService = ServiceFactory.getService(MeetingService.class);

    public MeetingDataForm(String label) {
        super(FormMode.NORMAL, label, new FormElement[]{
                new FormElement(Identifieable.Fields.ID, new JTextField()),
                new FormElement(Meeting.Fields.DATE, new JCalendarCombo(JCalendarCombo.DISPLAY_DATE
                        | JCalendarCombo.DISPLAY_TIME, true)),
        });

        JTextField id = (JTextField) getFormElement(Identifieable.Fields.ID).getComponent();
        id.setEnabled(false);
    }

    @Override
    public void save() throws ServiceException {
        Log.log(this, "save", "begin");
        formObject.setDate(getDate(Meeting.Fields.DATE));
        meetingService.write(formObject);
        Log.log(this, "save", "end");
    }

    @Override
    public void reset() {
        Log.log(this, "reset", "begin");
        if (formObject.getId() > 0) {
            onSelectionChanged(formObject);
        } else {
            onSelectionChanged(new Meeting());
        }
        Log.log(this, "reset", "end");
    }

    @Override
    public void cancel() {
        Log.log(this, "cancel", "begin");
        reset();
        Log.log(this, "cancel", "end");
    }

    @Override
    public boolean validation() {
        Log.log(this, "validation", "true");
        return true;
    }

    @Override
    public void onSelectionChanged(Object element) {
        Log.log(this, "selectionChanged", "element: %s", element);
        if (element instanceof Meeting) {
            formObject = (Meeting) element;
            updateTextField(Identifieable.Fields.ID, String.valueOf(formObject.getId()));
            updateCalendarCombo(Meeting.Fields.DATE, formObject.getDate());
        }
        Log.log(this, "selectionChanged", "end");
    }

}
